package com.huike.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huike.dto.ClueDto;
import com.huike.mapper.CluePoolMapper;
import com.huike.pojo.BusinessOpportunity;
import com.huike.result.PageResult;
import com.huike.service.CluePoolService;
import com.huike.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CluePoolServiceImpl extends ServiceImpl<CluePoolMapper, BusinessOpportunity> implements CluePoolService {
    private final CluePoolMapper cluePoolMapper;
    private final static String BEGIN="beginCreateTime";
    private final static String END="endCreateTime";
    private final static String CLUE_TYPE="1";
    private final static String CLUE_POOL_STATUS="4";

    @Override
    public PageResult getClueListPage(ClueDto clueDto) {//查询状态为4的伪线索，类型为1的线索
        Page<BusinessOpportunity> page = new Page<>(clueDto.getPageNum(), clueDto.getPageSize());
        LambdaQueryWrapper<BusinessOpportunity> wrapper = new LambdaQueryWrapper<>();
        String beginCreateTime = clueDto.getParams().get(BEGIN);
        String endCreateTime = clueDto.getParams().get(END);
        wrapper.eq(clueDto.getId()!=null,BusinessOpportunity::getId,clueDto.getId());
        wrapper.eq(StringUtils.isNotEmpty(clueDto.getPhone()),BusinessOpportunity::getPhone,clueDto.getPhone());
        wrapper.eq(StringUtils.isNotEmpty(clueDto.getChannel()),BusinessOpportunity::getChannel,clueDto.getChannel());
        wrapper.eq(BusinessOpportunity::getType,CLUE_TYPE);
        wrapper.eq(BusinessOpportunity::getStatus,CLUE_POOL_STATUS);

        if (StringUtils.isNotEmpty(beginCreateTime)&&StringUtils.isNotEmpty(endCreateTime)) {
            wrapper.between(BusinessOpportunity::getCreateTime, beginCreateTime, endCreateTime);
        }
        Page<BusinessOpportunity> res = super.page(page, wrapper);
        return new PageResult(res.getTotal(),res.getRecords());
    }

}
